package org.fatmansoft.teach.controllers.ViewController;

import org.fatmansoft.teach.Repository.ViewRepository.*;
import org.fatmansoft.teach.Repository.ViewRepository.DailyActivity.DailyActivityTypeRepository;
import org.fatmansoft.teach.Repository.ViewRepository.DailyActivity.StudentDailyActivityRepository;
import org.fatmansoft.teach.Repository.ViewRepository.HonorInfo.HonorTypeRepository;
import org.fatmansoft.teach.Repository.ViewRepository.HonorInfo.StudentHonorRepository;
import org.fatmansoft.teach.Repository.ViewRepository.PracticeInfo.PracticeTypeRepository;
import org.fatmansoft.teach.Repository.ViewRepository.PracticeInfo.StudentPracticeRepository;
import org.fatmansoft.teach.entity.ViewEntity.Course;
import org.fatmansoft.teach.entity.ViewEntity.DailyActivity.DailyActivityType;
import org.fatmansoft.teach.entity.ViewEntity.DailyActivity.StudentDailyActivity;
import org.fatmansoft.teach.entity.ViewEntity.HonorInfo.HonorType;
import org.fatmansoft.teach.entity.ViewEntity.HonorInfo.StudentHonor;
import org.fatmansoft.teach.entity.ViewEntity.PracticeInfo.StudentPractice;
import org.fatmansoft.teach.entity.ViewEntity.Student;
import org.fatmansoft.teach.entity.ViewEntity.StudentCourse;
import org.fatmansoft.teach.payload.request.DataRequest;
import org.fatmansoft.teach.payload.response.DataResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/getData")
//配置图表myChart3，需要传入的数据是student_practice表每个创新实践类型practice_type对应的名称以及其id的个数
public class StatisticController {
    @Autowired
    StudentRepository studentRepository;

    @Autowired
    StudentPracticeRepository studentPracticeRepository;

    @Autowired
    PracticeTypeRepository practiceTypeRepository;

    @Autowired
    StudentHonorRepository studentHonorRepository;

    @Autowired
    HonorTypeRepository honorTypeRepository;

    @Autowired
    StudentDailyActivityRepository studentDailyActivityRepository;

    @Autowired
    DailyActivityTypeRepository dailyActivityTypeRepository;

    @Autowired
    StudentLeavingRepository studentLeavingRepository;

    @Autowired
    StudentCourseRepository studentCourseRepository;

    @Autowired
    CourseRepository courseRepository;

    @Autowired
    StudentCampusCardCostRepository studentCampusCardCostRepository;

    @PostMapping("/getStatisticData")
    public DataResponse getStatisticData(@Valid @RequestBody DataRequest request) {
        Map<String, Map<String, Integer>> map = new HashMap<>();

        Map<String, Integer> data = new HashMap<>();

        //获取实践活动统计数据
        int total = 0;
        for (int i = 1; i <= 6; i++) {
            List<StudentPractice> practices = studentPracticeRepository.findAllByPracticeType(practiceTypeRepository.findById(i).get());
            int count = 0;
            if (practices != null) count = practices.size();
            total += count;
            data.put(Integer.toString(i), count);  //存放活动类型id和对应的统计数据
        }
        data.put("total", total);
        map.put("社会实践数据", data);

        //获取荣誉活动统计数据
        data = new HashMap<>();  //初始化统计数据的map
        total = 0;
        List<HonorType> honorTypes = honorTypeRepository.findAll();
//        for (HonorType honorType : honorTypes) {
        for (int i = 1; i <= honorTypes.size(); i++) {
            List<StudentHonor> honors = studentHonorRepository.findAllByHonorType(honorTypes.get(i - 1));
            int count = 0;
            if (honors != null) count = honors.size();
            total += count;
            data.put(Integer.toString(i), count);  //存放活动类型id和对应的统计数据
        }
        data.put("total", total);
        map.put("学生荣誉数据", data);

        //获取日常活动统计数据
        data = new HashMap<>();  //初始化统计数据的map
        total = 0;
        List<DailyActivityType> dailyActivityTypes = dailyActivityTypeRepository.findAll();
        for (int i = 1; i <= dailyActivityTypes.size(); i++) {
            List<StudentDailyActivity> activities = studentDailyActivityRepository.findAllByDailyActivityType(dailyActivityTypes.get(i - 1));
            int count = 0;
            if (activities != null) count = activities.size();
            total += count;
            data.put(Integer.toString(i), count);  //存放活动类型id和对应的统计数据
        }
        data.put("total", total);
        map.put("学生日常数据", data);

        //获取选课人数统计数据
        data = new HashMap<>();  //初始化统计数据的map
        total = 0;
        List<Course> courses = courseRepository.findAll();
        for (Course course : courses) {
            List<StudentCourse> studentCourses = studentCourseRepository.findAllByCourse(course);
            int count = 0;
            if (studentCourses != null) count = studentCourses.size();
            total += count;
            data.put(String.valueOf(course.getCourseName()), count);  //存放活动类型id和对应的统计数据
        }
        data.put("total", total);
        map.put("学生选课数据", data);

        //获取学生总绩点的数据
        data = new HashMap<>();  //初始化统计数据的map
        total = 0;
        List<Student> students = studentRepository.findAll();
        List<Double> GPAList = new ArrayList<>();
        for (Student student : students) {
            total++;
            Double totalGPA = this.getTotalGPA(student.getId());
            GPAList.add(totalGPA);
        }
        for (double i = 0; i < 5; i++) {
            int count = 0;
            for (double gpa : GPAList) {
                if (gpa >= i && gpa < i + 1) count++;  //左闭右开
            }
            String gpaStr = "" + i + "~" + (i + 1);
            data.put(gpaStr, count);
        }
        data.put("total", total);
        map.put("学生绩点分布", data);

        return new DataResponse("0", map);
    }

    public Double getTotalGPA(Integer studentId) {
        //获取学生所有选课记录
        List<StudentCourse> studentCourses = studentCourseRepository.findAllByStudentId(studentId);
        if (studentCourses != null) {  //获取学生选课记录对应的课程
            double totalCredit = 0;
            double totalCreditGpa = 0;
            for (StudentCourse studentCourse : studentCourses) {
                if (studentCourse.getGpa() == null) {
                    continue;
                }

                double credit = studentCourse.getCourse().getCredit();
                totalCredit += credit;
                totalCreditGpa += (credit * studentCourse.getGpa());
            }

            if (totalCredit != 0) {
                DecimalFormat d = new DecimalFormat("#.0");
//                System.out.println(d.format(totalCreditGpa / totalCredit));
                return Double.parseDouble(d.format(totalCreditGpa / totalCredit));
            }
        }

        return 0.0;
    }

}
